<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="支付方式">
              <j-dict-select-tag placeholder="请选择支付方式" v-model="queryParam.payType"
                dictCode="authorization_center_pay_type" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="订单状态">
              <j-dict-select-tag placeholder="请选择订单状态" v-model="queryParam.orderStatus"
                dictCode="authorization_center_order_status" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品类型">
              <j-dict-select-tag placeholder="请选择商品类型" v-model="queryParam.goodsType"
                dictCode="authorization_center_goods_type" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator"> -->
    <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button> -->
    <!-- <a-button type="primary" icon="download" @click="handleExportXls('授权中心订单')">导出</a-button> -->
    <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
    <!-- 高级查询区域 -->
    <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
    <!-- <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    <!-- </div> -->

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table ref="table" size="middle" :scroll="{ x: true }" bordered rowKey="id" :columns="columns"
        :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="goodsNumber" slot-scope="text, record">
          <div v-if="record.goodsType == 1">{{ record.yearNum ? (record.yearNum + '年') : record.userNumBack }}</div>
          <div v-else-if="record.goodsType == 2">1次</div>
          <div v-else-if="record.goodsType == 3">1次</div>
          <div v-else-if="record.goodsType == 4">1年</div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-if="record.orderStatus == 2" @click="handleHuidiao(record)">回调</a>

          <!-- <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>

    <xgh-authorization-center-orders-modal ref="modalForm" @ok="modalFormOk"></xgh-authorization-center-orders-modal>
    <!-- 回调弹窗 -->
    <a-modal :visible="visibleHuidiao" title="回调" ok-text="确定" cancel-text="取消" @ok="handleDialogHuidiao"
      @cancel="visibleHuidiao = false">
      <p>账套ID：</p>
      <a-input v-model="accountID" placeholder="请填写账套ID"></a-input>
    </a-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import XghAuthorizationCenterOrdersModal from './modules/XghAuthorizationCenterOrdersModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { postAction, getAction } from '@/api/manage'

export default {
  name: 'XghAuthorizationCenterOrdersList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    XghAuthorizationCenterOrdersModal
  },
  data() {
    return {
      description: '授权中心订单管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '订单编号',
          align: "center",
          dataIndex: 'orderNumber'
        },
        {
          title: '用户名',
          align: "center",
          dataIndex: 'userId_dictText'
        },
        {
          title: '支付方式',
          align: "center",
          dataIndex: 'payType_dictText'
        },
        {
          title: '订单状态',
          align: "center",
          dataIndex: 'orderStatus_dictText'
        },
        {
          title: '支付时间',
          align: "center",
          dataIndex: 'payTime'
        },
        {
          title: '应收金额',
          align: "center",
          dataIndex: 'orderMoney'
        },
        {
          title: '实收金额',
          align: "center",
          dataIndex: 'payMoney'
        },
        {
          title: '商品名称',
          align: "center",
          dataIndex: 'goodsName'
        },
        // {
        //   title:'商品id',
        //   align:"center",
        //   dataIndex: 'goodsId'
        // },
        {
          title: '商品类型',
          align: "center",
          dataIndex: 'goodsType_dictText'
        },
        {
          title: '商品数量',
          align: "center",
          dataIndex: 'goodsNumber',
          scopedSlots: { customRender: 'goodsNumber' }
        },
        // {
        //   title:'支付密钥',
        //   align:"center",
        //   dataIndex: 'paySecret'
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          width:100,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/XghAuthorizationCenter/xghAuthorizationCenterOrders/list",
        delete: "/XghAuthorizationCenter/xghAuthorizationCenterOrders/delete",
        deleteBatch: "/XghAuthorizationCenter/xghAuthorizationCenterOrders/deleteBatch",
        exportXlsUrl: "/XghAuthorizationCenter/xghAuthorizationCenterOrders/exportXls",
        importExcelUrl: "XghAuthorizationCenter/xghAuthorizationCenterOrders/importExcel",

      },
      dictOptions: {},
      superFieldList: [],
      visibleHuidiao: false,
      accountID: '',
      selectItem: '',
    }
  },
  created() {
    // this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    // 回调按钮
    handleHuidiao(record) {
      this.selectItem = record
      this.accountID = ''
      this.visibleHuidiao = true
    },
    // 确认回调
    handleDialogHuidiao() {
      if (this.accountID == this.selectItem.xghAccountBookId) {
        if (this.selectItem.goodsType == 1) {
              // 账套相关
              this.updateAccount(this.selectItem)
            } else if (this.selectItem.goodsType == 2) {
              // 系统恢复
              this.dataRestore(this.selectItem)
            } else if (this.selectItem.goodsType == 3) {
              // 反审（次）
              this.editConfigInfoByCode(this.selectItem)
            } else if (this.selectItem.goodsType == 4) {
              // 反审（年）
              this.editConfigInfoByCode(this.selectItem)
            }
      } else {
        this.$elMessage.warning('账套ID不正确！')
      }
    },

    // 账套处理接口
    updateAccount(model) {
      postAction('/accountbookinfo/xghAccountBook/updateAccountBook', {
        "buyType": model.buyType,
        "orderNum": 5000,
        "userNum": model.userNum,
        "xghAccountBookId": model.xghAccountBookId,
        "yearNum": model.yearNum,
      }).then(res => {
        if (res.code == 200) {
          this.visibleHuidiao = false
          this.$elMessage.success('回调成功！')
          this.updateOrderStatus()
        } else {
          this.$elMessage.error(res.message)
        }
      })
    },
    // 系统恢复接口
    dataRestore(model) {
      const that = this
      getAction('/sysConfig/systemReset/systemRestored', {
        xghAccountBookId: model.xghAccountBookId,
      }).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.visibleHuidiao = false
          this.$elMessage.success('回调成功！')
          this.updateOrderStatus()
        } else {
          this.$elMessage.error(res.message)
        }
      })
    },
    // 反审接口
    editConfigInfoByCode(model) {
      const that = this
      getAction('/sysConfig/systemConfig/editReVerifyConfig', {
        xghAccountBookId: model.xghAccountBookId,
        value: model.goodsType - 2,
      }).then(res => {
        if (res.code == 200) {
          this.visibleHuidiao = false
          this.$elMessage.success('回调成功！')
          this.updateOrderStatus()
        } else {
          this.$elMessage.error(res.message)
        }
      })
    },
    // 更新订单状态
    updateOrderStatus() {
      getAction('/pay/manualCallback', {
        orderNumber: this.selectItem.orderNumber,
      }).then(res => {
        if (res.success) {
          this.loadData()
        }
      })
    },
    

    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({ type: 'string', value: 'orderNumber', text: '订单编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'userId', text: '用户id', dictCode: '' })
      fieldList.push({ type: 'int', value: 'payType', text: '支付方式：1.微信 2.支付宝', dictCode: 'authorization_center_pay_type' })
      fieldList.push({ type: 'int', value: 'orderStatus', text: '订单状态：1.待付款 2.支付中，3.已完成 4.支付失败', dictCode: 'authorization_center_order_status' })
      fieldList.push({ type: 'datetime', value: 'payTime', text: '支付时间' })
      fieldList.push({ type: 'BigDecimal', value: 'orderMoney', text: '应收金额', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'payMoney', text: '实收金额', dictCode: '' })
      fieldList.push({ type: 'string', value: 'goodsName', text: '商品名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'goodsId', text: '商品id', dictCode: '' })
      fieldList.push({ type: 'int', value: 'goodsType', text: '商品类型：1.账套（版本） 2.用户数 3.云备份数', dictCode: 'authorization_center_goods_type' })
      fieldList.push({ type: 'int', value: 'goodsNumber', text: '商品数量（账套默认年，用户数默认无单位，云备份单位个）', dictCode: '' })
      fieldList.push({ type: 'int', value: 'paySecret', text: '支付密钥（用于银盛支付回调验证）', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>